package com.wyh.lotterysystem.controller;

import com.wyh.lotterysystem.common.pojo.CommonResult;
import com.wyh.lotterysystem.controller.response.BaseUserResponse;
import com.wyh.lotterysystem.service.UserService;
import com.wyh.lotterysystem.service.dto.UserListDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/base-user")
@Slf4j
public class BaseUserController {

    @Autowired
    UserService userService;

    @RequestMapping("/find-list")
    public CommonResult<List<BaseUserResponse>> findList(String identity){
        //日志打印
        log.info("获取人员列表......");

        //调用service层
        List<UserListDTO> userListDTOS = userService.findUserList(identity);

        //构建controller层返回值
        List<BaseUserResponse> responses = covertToBaseUserResponse(userListDTOS);

        //返回
        return CommonResult.success(responses);
    }

    private List<BaseUserResponse> covertToBaseUserResponse(List<UserListDTO> userListDTOS){
        //判断service返回的值是否为null，
        //如果为null就直接返回。
        if (null == userListDTOS){
            return List.of();//创建一个空列表并且该列表具有不可变性
        }

        //用service返回的对象构造出Controller层需要返回的对象

        return userListDTOS.stream().map(userListDTO -> {
            BaseUserResponse response = new BaseUserResponse();
            BeanUtils.copyProperties(userListDTO,response);
            return response;
        }).toList();
    }
}
